package com.yy.module.service;

import java.util.List;
import org.springframework.stereotype.Service;
import com.yy.base.common.service.BaseService;
import com.yy.module.entity.ModuleField;
import com.yy.module.entity.ModuleInfo;
import com.yy.module.entity.ModuleQueryField;
import com.yy.module.entity.Table;

@Service("moduleService")
@SuppressWarnings("unchecked")
public class ModuleService extends BaseService {
	/**
	 * 查询模块展示信息
	 */
	public List getModuleFieldList(String moduleId) {
		List<ModuleField> list = dao.find("from ModuleField where isActive = 'Y' and moduleId = ? order by orderBy", moduleId);
		for (ModuleField f : list) {
			if (f.getTableId() != null) {
				f.setTableName(dao.get(Table.class, f.getTableId()).getText());
			}
		}
		return list;
	}
	/**
	 * 查询模块展示信息
	 */
	public List getModuleQueryList(String moduleId){
		List<ModuleQueryField> list = dao.find("from ModuleQueryField where isActive = 'Y' and moduleId = ? order by orderBy", moduleId);
		for (ModuleQueryField f : list) {
			if (f.getTableId() != null) {
				f.setTableName(dao.get(Table.class, f.getTableId()).getText());
			}
		}
		return list;
	}
	/**
	 * 查询模块信息，生成模块树
	 */
	public List getModuleList(ModuleInfo module){
		String hql = "from ModuleInfo m where m.isActive = 'Y' and (m.parentId is null or m.parentId = ?)";
		return getModuleList(hql, "0");
	}
	/**
	 * 查询模块信息，生成模块树
	 */
	private List getModuleList(String hql, String id) {
		List<ModuleInfo> list = dao.find(hql, id);
		List l = null;
		for (ModuleInfo m : list) {
			l = getModuleList("from ModuleInfo where isActive = 'Y' and parentId = ?", m.getId());
			if (l != null && l.size() > 0) {
				m.setChildren(l);
			}
		}
		return list;
	}
	/**
	 * 查询模块信息
	 */
	public ModuleInfo getModuleInfoById(String id) {
		return dao.get(ModuleInfo.class, id);
	}
	/**
	 * 查询模块信息
	 */
	public ModuleInfo getModuleByCode(String code) {
		return (ModuleInfo)dao.findUnique("from ModuleInfo where isActive = 'Y' and code = ?", code);
	}
	/**
	 * 查询模块字段信息
	 */
	public ModuleField getModuleField(String moduleId, String tableId, String fieldName) {
		return (ModuleField)dao.findUnique("from ModuleField where tableId = ? and fieldName = ? and moduleId = ?", tableId, fieldName, moduleId);
	}
	/**
	 * 查询模块字段信息
	 */
	public ModuleQueryField getModuleQueryField(String moduleId, String tableId, String fieldName) {
		return (ModuleQueryField)dao.findUnique("from ModuleQueryField where tableId = ? and fieldName = ? and moduleId = ?", tableId, fieldName, moduleId);
	}
	/**
	 * 查询模块字段信息
	 */
	public ModuleField getModuleFieldById(String id) {
		return dao.get(ModuleField.class, id);
	}
	/**
	 * 查询模块查询信息
	 */
	public ModuleQueryField getModuleQueryById(String id) {
		return dao.get(ModuleQueryField.class, id);
	}
}